home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* Application: PixMap2PixPat2ppat */
- /* */
- /* Description: This snippet shows how to convert a 'icl8' image to a */
- /* PixMap image and then to a PixPat and then finally to */
- /* a 'ppat' resource. In this example, the 'ppat' */
- /* resource is saved-off into a resource file. */
- /* */
- /* Files: PixMap2PixPat2ppat.π */
- /* PixMap2PixPat2ppat.c */
- /* PixMap2PixPat2ppat.π.rsrc */
- /* */
- /* Programmer: Edgar Lee */
- /* Organization: Apple Computer, Inc. */
- /* Department: Developer Technical Support, DTS */
- /* Language: C (Think C version 5.0.2) */
- /* Date Created: 09-02-92 */
- /* */
- /****************************************************************************/
-
- /* Constant Declarations */
-
- #define WWIDTH 320
- #define WHEIGHT 160
-
- #define WLEFT (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
- #define WTOP (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
-
- /* Global Variable Definitions */
-
- WindowPtr gWindow;
-
- void initMac();
- void createWindow();
- void doEventLoop();
- void drawWindow();
-
- void createPixMap();
-
- void PixMap2PixPat();
- void PixPat2ppat();
- void ppat2file();
-
- main()
- {
- PixMapHandle pixmap;
- PixPatHandle pixpat;
-
- initMac();
-
- pixmap = (PixMapHandle)NewHandle( sizeof( PixMap ) );
- pixpat = NewPixPat();
-
- createWindow();
-
- createPixMap( pixmap );
- PixMap2PixPat( pixmap, pixpat );
- PixPat2ppat( pixpat );
-
- doEventLoop( pixpat );
- }
-
- void initMac()
- {
- MaxApplZone();
-
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( nil );
- InitCursor();
- FlushEvents( 0, everyEvent );
- }
-
- void createWindow()
- {
- Rect rect;
-
- SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
-
- gWindow = NewCWindow( 0L, &rect, "\pPixMap2PixPat2ppat", true, noGrowDocProc,
- (WindowPtr)-1L, true, 0L );
- SetPort( gWindow );
- }
-
- void createPixMap( pixmap )
- PixMapHandle pixmap;
- {
- Handle iclHandle;
- char depth;
- Rect rect;
-
- SetRect( &rect, 0, 0, 32, 32 );
- depth = 8;
-
- /* Create a pixmap from the icl8 pixel image. */
-
- iclHandle = GetResource( 'icl8', 129 );
-
- HLock( iclHandle );
- HNoPurge( iclHandle );
-
- (**pixmap).baseAddr = *iclHandle;
- (**pixmap).rowBytes = (((rect.right - rect.left) * depth) / 8) | 0x8000;
- (**pixmap).bounds = rect;
- (**pixmap).pmVersion = 0;
- (**pixmap).packType = 0;
- (**pixmap).packSize = 0;
- (**pixmap).hRes = 0x00480000;
- (**pixmap).vRes = 0x00480000;
- (**pixmap).pixelSize = depth;
- (**pixmap).planeBytes = 0;
- (**pixmap).pmReserved = 0;
- (**pixmap).pixelType = 0;
- (**pixmap).cmpCount = 1;
- (**pixmap).cmpSize = depth;
- (**pixmap).pmTable = GetCTable( depth );
- }
-
- void PixMap2PixPat( pixmap, pixpat )
- PixMapHandle pixmap;
- PixPatHandle pixpat;
- {
- Handle image;
- long imageSize;
-
- (**pixpat).patMap = pixmap;
-
- imageSize = (**(**pixpat).patMap).rowBytes *
- ((**(**pixpat).patMap).bounds.bottom -
- (**(**pixpat).patMap).bounds.top);
-
- PtrToHand( (**pixmap).baseAddr, &image, imageSize );
- (**pixpat).patData = image;
- }
-
- void PixPat2ppat( pixpat )
- PixPatHandle pixpat;
- {
- Handle ppat;
- PixPatHandle pixpatCopy;
- long pixmapSize, pixpatSize, imageSize, ctableSize;
-
- pixpatCopy = NewPixPat();
- CopyPixPat( pixpat, pixpatCopy );
-
- pixmapSize = sizeof( PixMap );
- pixpatSize = sizeof( PixPat);
- imageSize = (**(**pixpatCopy).patMap).rowBytes *
- ((**(**pixpatCopy).patMap).bounds.bottom -
- (**(**pixpatCopy).patMap).bounds.top);
- ctableSize = sizeof( ColorTable ) +
- (**(**(**pixpatCopy).patMap).pmTable).ctSize *
- sizeof( ColorSpec );
-
- /********************************************/
- /* Allocate memory for the 'ppat' resource. */
- /********************************************/
-
- ppat = NewHandle( pixpatSize + pixmapSize + imageSize + ctableSize );
-
- /**********************/
- /* Fill the resource. */
- /**********************/
-
- BlockMove( *(**(**pixpatCopy).patMap).pmTable,
- *ppat + pixpatSize + pixmapSize + imageSize, ctableSize );
- (**(**pixpatCopy).patMap).pmTable = (CTabHandle)(pixpatSize + pixmapSize + imageSize);
-
- BlockMove( *(**pixpatCopy).patData, *ppat + sizeof( PixPat ) + sizeof( PixMap), imageSize );
- (**pixpatCopy).patData = (Handle)(pixpatSize + pixmapSize);
-
- BlockMove( *(**pixpatCopy).patMap, *ppat + sizeof( PixPat ), pixmapSize );
- (**pixpatCopy).patMap = (PixMapHandle)pixpatSize;
-
- BlockMove( *pixpatCopy, *ppat, pixpatSize );
-
- /************************************************/
- /* Finally, save the 'ppat' resource to a file. */
- /************************************************/
-
- ppat2file( ppat, "\pppat file" );
- }
-
- void ppat2file( ppat, fname )
- Handle ppat;
- Str255 fname;
- {
- int refNum;
-
- CreateResFile( fname );
-
- if (ResError() == noErr)
- {
- refNum = OpenResFile( fname );
-
- if (refNum != -1)
- {
- AddResource( ppat, 'ppat', UniqueID( 'ppat' ), "\pMy ppat" );
- CloseResFile( refNum );
- }
- }
- }
-
- void drawWindow( pixpat )
- PixPatHandle pixpat;
- {
- FillCRect( &(*gWindow).portRect, pixpat );
-
- TextFont( times );
- TextSize( 72 );
- TextMode( srcOr );
-
- ForeColor( blackColor );
- MoveTo( 80, 100 );
- DrawString( "\pDone." );
-
- ForeColor( yellowColor );
- MoveTo( 75, 95 );
- DrawString( "\pDone." );
- }
-
- void doEventLoop( pixpat )
- PixPatHandle pixpat;
- {
- EventRecord event;
- WindowPtr window;
- short clickArea;
- Rect screenRect;
-
- for (;;)
- {
- if (WaitNextEvent( everyEvent, &event, 0, nil ))
- {
- if (event.what == mouseDown)
- {
- clickArea = FindWindow( event.where, &window );
-
- if (clickArea == inDrag)
- {
- screenRect = (**GetGrayRgn()).rgnBBox;
- DragWindow( window, event.where, &screenRect );
- }
- else if (clickArea == inContent)
- {
- if (window != FrontWindow())
- SelectWindow( window );
- }
- else if (clickArea == inGoAway)
- if (TrackGoAway( window, event.where ))
- return;
- }
- else if (event.what == updateEvt)
- {
- window = (WindowPtr)event.message;
- SetPort( window );
-
- BeginUpdate( window );
- drawWindow( pixpat );
- EndUpdate( window );
- }
- }
- }
- }